
//****************************************************************************************
//*
//* File Name: bsp_temphumiditymodule_handler.c 
//* Level: handler
//* Version: v4.0
//* Date: 2024-07-09 13:05:10
//* Author: su
//* 
//****************************************************************************************/

//******************************** Includes *********************************//

#include "bsp_temphumiditymodule_handler.h"
#include "main.h"
#define TMNE_STAMPINIT 	10000
#define WAITTIME        0xffffffffUL
#define QUEUEHANDLER    thermhumiditymodule_handler.rtos_yeild->handler
#define TEMP            temp_humi_event.temp
#define HUMI            temp_humi_event.humi
static int8_t thermhumiditymodule_handler_inst( struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler,
    
                        bsp_aht21_t                  * aht21_instance, // AHT21的实体实例

					    iic_driver_interface_t       * iic_instance,   // IIC的实体实例
					
					    system_timebase_interface_t  * timetimebase,       // 时基
					
					    ec_bsp_os_interface_t        * rtos_yeild                       );
static int8_t thermhumiditymodule_handler_deinst(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler);
static int8_t thermhumiditymodule_handler_init(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler);
static int8_t thermhumiditymodule_handler_deinit(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler);
static ec_bsp_thermhumiditymodule_handler_t thermhumiditymodule_handler;
static temp_humi_event_t eventhandler;
//线程函数
void thermhumiditymodule_thread(void *para)
{		
	//指向传进来的参数
	ec_bsp_thermhumiditymodule_handler_public_t *handler_public;
	handler_public = (ec_bsp_thermhumiditymodule_handler_public_t*)para;
	
	bsp_aht21_t 										  bsp_aht21;	
	thermhumiditymodule_handler.aht21_instance     		= &bsp_aht21;
	thermhumiditymodule_handler.pfinst 					= thermhumiditymodule_handler_inst;
	thermhumiditymodule_handler.pfdeinit 				= thermhumiditymodule_handler_deinit;
	if( 0 != thermhumiditymodule_handler.pfinst( &thermhumiditymodule_handler,
												 &bsp_aht21,
												 &handler_public->iic_instance,
												 &handler_public->timebase_interface,
												 &handler_public->rtos_yeild))
	{
		thermhumiditymodule_handler.pfdeinit(&thermhumiditymodule_handler);
	}	
	//创建保存数据的结构体
	temp_humi_event_t temp_humi_event;
	//创建队列空间
	thermhumiditymodule_handler.rtos_yeild->queue_create( 3, sizeof(temp_humi_event_t),&QUEUEHANDLER);
	static uint32_t timecount = 0;
	static uint8_t eventflag = 0;
	static uint8_t timecountinitflag = 0;
	while(1)
	{			
		thermhumiditymodule_handler.rtos_yeild->queue_read(&temp_humi_event, WAITTIME,QUEUEHANDLER);
		if( 0 == eventflag)
		{
			eventhandler = temp_humi_event; //传进来的数值等于这个eventhandler
			temp_humi_event.timestamp = TMNE_STAMPINIT; //让他等于一个很大的数值好让下边判断进入else分支
			eventflag = 1;
		}
		
		if(temp_humi_event.timestamp < (timecount + temp_humi_event.lifetime)) //判断当前时间是否小于上一次记录的时间+新鲜度
		{
			//如果第一个调用的线程只要温度，那么第二个线程每隔1分钟读取温湿度，那么每次读取的湿度是不是都是最初始的数值呢？
			
//			if(NULL == eventhandler.temp)
//			{
//				thermhumiditymodule_handler.aht21_instance->pfread_temperature(&eventhandler.temp);
//			}
//			if(NULL == eventhandler.humi)
//			{
//				thermhumiditymodule_handler.aht21_instance->pfread_temperature(&eventhandler.humi);
//			}
			eventhandler.callback = temp_humi_event.callback;     //把回调函数赋值给发送的结构体
			eventhandler.lifetime = temp_humi_event.lifetime;	  //新鲜度也赋值给发送结构体
			switch(temp_humi_event.type_of_data)
			{
				case TEMP_HUMI_EVENT_TYPE_TEMP:
					eventhandler.callback(&eventhandler.temp,0);	//回调出去
					HUMI = 0;
					break;
				case TEMP_HUMI_EVENT_TYPE_HUMI:
					eventhandler.callback(0,&eventhandler.humi);	//回调出去
					TEMP = 0;
					break;
				case TEMP_HUMI_EVENT_TYPE_BOTH:
					eventhandler.callback(&eventhandler.temp,&eventhandler.humi);	//回调出去
					break;
			}
			
		}
		else
		{
			if(0 ==timecountinitflag)
			{
				timecount = 0;                 //第一次让他等于0，因为第一次temp_humi_event.timestamp初始数值等于10000
				timecountinitflag =1;	       //赋值了的话上边判断下次肯定要进入上边分支
			}
			else
			{
			timecount = temp_humi_event.timestamp;  //后续进入这里都可以正常赋值了
			}
			
			switch(temp_humi_event.type_of_data)
			{
				case TEMP_HUMI_EVENT_TYPE_TEMP:
					thermhumiditymodule_handler.aht21_instance->pfread_temphumidata(&TEMP,&HUMI);
					eventhandler = temp_humi_event;
					HUMI = 0;
					break;
				case TEMP_HUMI_EVENT_TYPE_HUMI:
					thermhumiditymodule_handler.aht21_instance->pfread_temphumidata(&TEMP,&HUMI);
					eventhandler = temp_humi_event;
					TEMP = 0;
					break;
				case TEMP_HUMI_EVENT_TYPE_BOTH:
					thermhumiditymodule_handler.aht21_instance->pfread_temphumidata(&TEMP,&HUMI);
					eventhandler = temp_humi_event;
					break;
			}
			
			temp_humi_event.callback(&TEMP,&HUMI);	
		}
	}
	
}

//初始化hangdler资源
static int8_t thermhumiditymodule_handler_inst( 
						struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler,
    
                        bsp_aht21_t                  * aht21_instance, // AHT21的实体实例

					    iic_driver_interface_t       * iic_instance,   // IIC的实体实例
					
					    system_timebase_interface_t  * timetimebase,       // 时基
					
					    ec_bsp_os_interface_t        * rtos_yeild                       )
{
	ec_bsp_thermhumiditymodule_handler->pfdeinst = thermhumiditymodule_handler_deinst;
	ec_bsp_thermhumiditymodule_handler->iic_instance    					  = iic_instance;
	
	if( ec_bsp_thermhumiditymodule_handler->iic_instance 				==NULL ||
		ec_bsp_thermhumiditymodule_handler->iic_instance->pfinit 		==NULL ||
		ec_bsp_thermhumiditymodule_handler->iic_instance->pfdeinit	    ==NULL ||
		ec_bsp_thermhumiditymodule_handler->iic_instance->pfwrite_reg 	==NULL ||
		ec_bsp_thermhumiditymodule_handler->iic_instance->pfread_reg 	==NULL 			)
	{
		ec_bsp_thermhumiditymodule_handler->pfdeinst(ec_bsp_thermhumiditymodule_handler);
		return -1;
	}
	
	ec_bsp_thermhumiditymodule_handler->timebase_interface					  =	timetimebase;
	
	if( ec_bsp_thermhumiditymodule_handler->timebase_interface 				==NULL ||
		ec_bsp_thermhumiditymodule_handler->timebase_interface->delay_ms	==NULL ||
		ec_bsp_thermhumiditymodule_handler->timebase_interface->delay_us 	==NULL 		)
	{
		ec_bsp_thermhumiditymodule_handler->pfdeinst(ec_bsp_thermhumiditymodule_handler);
		return -2;
	}
	ec_bsp_thermhumiditymodule_handler->rtos_yeild                            = rtos_yeild;
	
	ec_bsp_thermhumiditymodule_handler->pfinit = thermhumiditymodule_handler_init;
	ec_bsp_thermhumiditymodule_handler->pfdeinit = thermhumiditymodule_handler_deinit;
	ec_bsp_thermhumiditymodule_handler->pfinit(ec_bsp_thermhumiditymodule_handler);
	return 0;
}


//去初始化初始化hangdler资源
static int8_t thermhumiditymodule_handler_deinst(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler)
{
	ec_bsp_thermhumiditymodule_handler->pfdeinit(ec_bsp_thermhumiditymodule_handler);
	ec_bsp_thermhumiditymodule_handler = NULL;
	return 0;
}

static int8_t thermhumiditymodule_handler_init(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler)
{
	aht21_inst( ec_bsp_thermhumiditymodule_handler->aht21_instance,
				ec_bsp_thermhumiditymodule_handler->iic_instance,
				ec_bsp_thermhumiditymodule_handler->timebase_interface);
	
	return 0;
}

//去初始化初始化hangdler底层资源
static int8_t thermhumiditymodule_handler_deinit(struct ec_bsp_thermhumiditymodule_handler * ec_bsp_thermhumiditymodule_handler)
{
	ec_bsp_thermhumiditymodule_handler->aht21_instance->pfdeinit();
	return 0;
}

//外部获取数据接口
int8_t mhumiditymodule_event_read(temp_humi_event_t * event)
{
		event->timestamp = thermhumiditymodule_handler.rtos_yeild->getnowtime();
		thermhumiditymodule_handler.rtos_yeild->queue_send(event,WAITTIME,QUEUEHANDLER);

	return 0;
}

//  end handler
